<?php  
  
namespace Notification;  
  
//引入核心文件  
require_once('AndroidBroadcast.php');  
require_once('AndroidFilecast.php');  
require_once('AndroidGroupcast.php');  
require_once('AndroidUnicast.php');  
require_once('AndroidCustomizedcast.php');  
require_once('IOSBroadcast.php');  
require_once('IOSFilecast.php');  
require_once('IOSGroupcast.php');  
require_once('IOSUnicast.php');  
require_once('IOSCustomizedcast.php');  
  
  
class Sms {  
    protected $appkey           = NULL;   
    protected $appMasterSecret     = NULL;  
    protected $timestamp        = NULL;  
    protected $validation_token = NULL;  
  
    function __construct($key, $secret) {  
        $this->appkey = $key;  
        $this->appMasterSecret = $secret;  
        $this->timestamp = strval(time());  
    }  
  
    /** 
     * Android推送—广播 
     * @param $title string 推送消息标题 
     * @param $content string 推送消息内容 
     * @return mixed 
     */  
    function sendAndroidBroadcast($title,$content) {  
        try {  
            $brocast = new AndroidBroadcast();  
            $brocast->setAppMasterSecret($this->appMasterSecret);  
            $brocast->setPredefinedKeyValue("appkey",           $this->appkey);  
            $brocast->setPredefinedKeyValue("timestamp",        $this->timestamp);  
            $brocast->setPredefinedKeyValue("ticker",           "Android broadcast ticker");  
            $brocast->setPredefinedKeyValue("title",            $title);  
            $brocast->setPredefinedKeyValue("text",             $content);  
            $brocast->setPredefinedKeyValue("after_open",       "go_app");  
            $brocast->setPredefinedKeyValue("production_mode", "true");  
            $brocast->setExtraField("test", "helloworld");  
            print("Sending broadcast notification, please wait...\r\n");  
            return $brocast->send();  
            print("Sent SUCCESS\r\n");  
        } catch (Exception $e) {  
            print("Caught exception: " . $e->getMessage());  
        }  
    }  
  
    /** 
     * Android推送—单播 
     * @param $title string 推送消息标题 
     * @param $content string 推送消息内容 
     * @param $tokens array 设备的token值 
     * @return mixed 
     */  
    function sendAndroidUnicast($title,$content,$tokens) {  
        try {  
            $unicast = new AndroidUnicast();  
            $unicast->setAppMasterSecret($this->appMasterSecret);  
            $unicast->setPredefinedKeyValue("appkey",           $this->appkey);  
            $unicast->setPredefinedKeyValue("timestamp",        $this->timestamp);  
            $unicast->setPredefinedKeyValue("device_tokens",    $tokens);  
            $unicast->setPredefinedKeyValue("ticker",           "Android unicast ticker");  
            $unicast->setPredefinedKeyValue("title",            $title);  
            $unicast->setPredefinedKeyValue("text",             $content);  
            $unicast->setPredefinedKeyValue("after_open",       "go_app");  
            $unicast->setPredefinedKeyValue("production_mode", "true");  
            $unicast->setExtraField("test", "helloworld");  
            print("Sending unicast notification, please wait...\r\n");  
            return $unicast->send();  
            print("Sent SUCCESS\r\n");  
        } catch (Exception $e) {  
            print("Caught exception: " . $e->getMessage());  
        }  
    }
    //<pre name="code" class="php">
//<span style="white-space:pre">    </span>
    /**
     * IOS推送—广播 
     * @param $title string 推送消息标题 
     * @param $content string 推送消息内容 
     * @return mixed 
     */  
    function sendIOSBroadcast($title,$content) {  
        try {  
            $brocast = new IOSBroadcast();  
            $brocast->setAppMasterSecret($this->appMasterSecret);  
            $brocast->setPredefinedKeyValue("appkey",           $this->appkey);  
            $brocast->setPredefinedKeyValue("timestamp",        $this->timestamp);  
            $brocast->setPredefinedKeyValue("alert", $title);  
            $brocast->setPredefinedKeyValue("badge", 0);  
            $brocast->setPredefinedKeyValue("sound", "chime");  
            $brocast->setPredefinedKeyValue("production_mode", "false");  
            $brocast->setCustomizedField("test", $content);  
            print("Sending broadcast notification, please wait...\r\n");  
            return $brocast->send();  
            print("Sent SUCCESS\r\n");  
        } catch (Exception $e) {  
            print("Caught exception: " . $e->getMessage());  
        }  
    }  
  
    /** 
     * IOS推送—单播 
     * @param $title string 推送消息标题 
     * @param $content string 推送消息内容 
     * @param $tokens array 设备的token值 
     * @return mixed 
     */  
    function sendIOSUnicast($title,$content,$tokens) {  
        try {  
            $unicast = new IOSUnicast();  
            $unicast->setAppMasterSecret($this->appMasterSecret);  
            $unicast->setPredefinedKeyValue("appkey",           $this->appkey);  
            $unicast->setPredefinedKeyValue("timestamp",        $this->timestamp);  
            $unicast->setPredefinedKeyValue("device_tokens",    $tokens);  
            $unicast->setPredefinedKeyValue("alert", $title);  
            $unicast->setPredefinedKeyValue("badge", 0);  
            $unicast->setPredefinedKeyValue("sound", "chime");  
            $unicast->setPredefinedKeyValue("production_mode", "false");  
            $unicast->setCustomizedField("test", $content);  
            print("Sending unicast notification, please wait...\r\n");  
            return $unicast->send();  
            print("Sent SUCCESS\r\n");  
        } catch (Exception $e) {  
            print("Caught exception: " . $e->getMessage());  
        }  
  }//<pre name="code" class="php">
}